#include "Include.h"
vu8 R_PanTemp;
vu16 TargetTemp;

void UI_Task()
{
    if (ControlSet_App.flagBit.on_off == 0)
    {
        bMachineOnState = OFF; // power down
    }
    else if (PowerStep_buf >= 1)
    {
        if (OFF == bMachineOnState) // Power on
        {
            bMachineOnState = ON;
            PowerOnProc();          
            // Clear error flag
            bErrPowerOff = 0;
        }
		
		
        if (PowerStep != PowerStep_buf)
        {
        	//Power Limiter
        	if(PowerStep_buf > POWER_STEP_MIN)// > 1000W
        	{
        		if(1==bLowPower)
			  	{
				  	bModeChanged=1;	//Low power to high power flag,Wait for the low power execution to end before proceeding to high power
			  	}
        		if (PowerStep_buf>=POWER_STEP_MAX) 	//power MAX 2000W
				{
					PowerStep=POWER_STEP_MAX;
				}
				else
				{
					PowerStep = PowerStep_buf;
				}
                // 1600W Frequency shaking Enter hardware frequency shaking mode flag
                if (PowerStep >= (FREQUENCY_JITTERING_OPEN/20) )		
                {
                	#if FREQUENCY_JITTERING_ENABLE == 1
                	  HW_State_type1.bits.HW_Approach = 1;
                	#endif                  
                }
                else
                {
                    if (HW_State_type1.bits.HW_Approach)
                    {
                        HW_State_type1.bits.HW_Approach = 0;

                        // Change the initial value of PPG width to prevent power surge
                        HW_State_type1.bits.HW_Approach_Disable = 1;
                    }
                }						
        	}
        	//Low power continuous heating
        	else//<= 1000W
        	{
        		if (PowerStep_buf >= LOW_POWER_STEP_MAX)
	            {
	                PowerStep = LOW_POWER_STEP_MAX;//Low power mode maximum power
	            }
	            else if(PowerStep_buf <= LOW_POWER_STEP_MIN)
	            {
	            	PowerStep = LOW_POWER_STEP_MIN;//Low power mode minimum power
	            }
	            else
	            {
	            	PowerStep = PowerStep_buf;
	            } 
	            //---------------------------
                bLowPower = 1;
                if (0 == Statu_LowPow_Work)
                {
                    Statu_LowPow_Work = 1;
                }          	           	            
        	}        
            bPowerStepChanged = 1;
        }
    }
    if (OFF == bMachineOnState)
    {
        PowerOffProc();
        bErrPowerOff = 0;
        bLowPower = 0;
        Statu_LowPow_Work = 0;
    }

    //------- Beep
    if (0 != ControlSet_App.flagBit.beep)
    {
	    Beep_Cmd = ControlSet_App.flagBit.beep;
	
	    ControlSet_App.flagBit.beep = 0;
    }
    Beep_Task();
    //------- Fan
    Fan_Task();   
}



void LowPow_PowerUpdata()
{
	//------------------------Low power control--------------------             
//	if (PowerStep >= 10 && PowerStep < 20)   // 200-400		Low power range 1
//    {
//    	LowPow_ON = 2; //	Work done in 1 of the 4 AC cycles 
//    }
//    else if (PowerStep >= 20 && PowerStep < 40)   //500-700
//    {
//        LowPow_ON = 3; //	Work done in 2 out of 4 AC cycles 
//    }
//    else if (PowerStep >=40 && PowerStep <= 50)  //800-900
//    {	
//		LowPow_ON = 4; //	Work done in 3 out of 4 AC cycles 
//    }  
	
	if (LimTargetPower >= 200 && LimTargetPower < 400)   // 200-400		Low power range 1
    {
    	LowPow_ON = 2; //	Work done in 1 of the 4 AC cycles 
    }
    else if (LimTargetPower >= 400 && LimTargetPower < 800)   //500-700
    {
        LowPow_ON = 3; //	Work done in 2 out of 4 AC cycles 
    }
    else if (LimTargetPower >=800 && LimTargetPower <= 1000)  //800-900
    {	
		LowPow_ON = 4; //	Work done in 3 out of 4 AC cycles 
    }  

    LowPow_Cycle = C_LOWPOW_CYCLE; // one power cycle using 4 AC cycle =40 ms˙ 	
}


